//
//  OCRReaderViewController.m
//  OCRReader
//
//  Created by Tomasz Krasnyk on 1/14/10.
//  Copyright Polidea 2010. All rights reserved.
//

#import "UIOperationsExampleViewController.h"
#import "ImageOperationsExampleAppDelegate.h"
#import "UIImage+Operations.h"

#define fToolbarHeight 44.0f 
#define fProcessingImageHeight 240.0f


@interface UIOperationsExampleViewController()
@property (nonatomic, retain) UIImageView *viewWithSelectedImage;
@property (nonatomic, retain) UIImage *imageToOperateOn;

@property (nonatomic, retain) UIBarButtonItem *doConvertToGrayScale;
@property (nonatomic, retain) UIBarButtonItem *doSkewCorrection;
@end


@implementation UIOperationsExampleViewController
@synthesize viewWithSelectedImage;

@synthesize doConvertToGrayScale;
@synthesize doSkewCorrection;
@synthesize imageToOperateOn;

#pragma mark -
#pragma mark get/set
//---------------------------------------------------------------------------------------------
- (void) setImageToOperateOn:(UIImage *) newImage {
	if(newImage != imageToOperateOn){
		[imageToOperateOn release];
		imageToOperateOn = [newImage retain];
		
		BOOL bottomButtonsHidden = imageToOperateOn != nil;
		[self.doConvertToGrayScale setEnabled:bottomButtonsHidden];
		[self.doSkewCorrection setEnabled:bottomButtonsHidden];
		[self.viewWithSelectedImage setImage:imageToOperateOn];
	}
}
//---------------------------------------------------------------------------------------------
#pragma mark -
- (id) init {
	if(self = [super init]){
	}
	return self;
}

- (NSArray *) newToolbarItems {
	UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
	
	self.doConvertToGrayScale = [[UIBarButtonItem alloc] initWithTitle:@"Gray" style:UIBarButtonItemStyleBordered target:self action:@selector(doConvertToGrayScaleClicked)];	
	[self.doConvertToGrayScale setEnabled:self.imageToOperateOn != nil];
	[self.doConvertToGrayScale release];
	
	self.doSkewCorrection = [[UIBarButtonItem alloc] initWithTitle:@"Rotate" style:UIBarButtonItemStyleBordered target:self action:@selector(doSkewCorrectionClicked)];
	[self.doSkewCorrection setEnabled:self.imageToOperateOn != nil];
	[self.doSkewCorrection release];
	
	NSArray *toolbarItems = [[NSArray alloc] initWithObjects:flexibleSpace, self.doConvertToGrayScale, self.doSkewCorrection, nil];
	[flexibleSpace release];
	return toolbarItems;
}

- (void) loadView {
	[super loadView];
	
	self.viewWithSelectedImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - fToolbarHeight)];
	[self.viewWithSelectedImage setContentMode:UIViewContentModeScaleAspectFit];
	[self.viewWithSelectedImage setImage:self.imageToOperateOn];
	[self.viewWithSelectedImage release];
	[self.view addSubview:self.viewWithSelectedImage];
	
	self.title = NSLocalizedString(@"Image Processing", @"title of the main controller");
}

- (void) viewDidLoad {
	NSArray *toolbarItems = [self newToolbarItems];
	[self setToolbarItems:toolbarItems animated:YES];
	[toolbarItems release];
	
	[self.navigationController setToolbarHidden:NO animated:YES];
	
	UIBarButtonItem *pickImage = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"camera.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(pickImageClicked)];
	self.navigationItem.rightBarButtonItem = pickImage;
	[pickImage release];
}

- (void) doConvertToGrayScaleClicked {
	self.imageToOperateOn = [UIImage convertTo8bppGrayscaleFromImage:self.imageToOperateOn];
	[self.viewWithSelectedImage setImage:self.imageToOperateOn];
}

- (void) doSkewCorrectionClicked {
	CorrectingSkewView *obj = [[CorrectingSkewView alloc] initWithOriginalImage:self.imageToOperateOn];
	[obj setDelegate:self];
	[self.navigationController pushViewController:obj animated:YES];
	[obj release];
}

- (void) pickImageClicked {
	UIActionSheet *as = [[UIActionSheet alloc] init];
	[as setTitle:NSLocalizedString(@"Choose source of the image", nil)];
	[as setDelegate:self];
	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
		[as addButtonWithTitle:NSLocalizedString(@"Camera", nil)];
	}
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
		[as addButtonWithTitle:NSLocalizedString(@"Photo library", nil)];
	}
	[as addButtonWithTitle:NSLocalizedString(@"cancel", nil)];
	[as setCancelButtonIndex:[as numberOfButtons]-1];
	UIWindow *window = [(ImageOperationsExampleAppDelegate *)[[UIApplication sharedApplication] delegate] window];
	[as showInView:window];
	[as release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
	[picker dismissModalViewControllerAnimated:YES];
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	if(image.imageOrientation == 3){	//rotate right by 90
		image = [UIImage rotateImage:image andRotateAngle:UIImageOrientationDown];
	}
	self.imageToOperateOn = image;
	[pool release];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.viewWithSelectedImage = nil;
	
	self.doConvertToGrayScale = nil;
	self.doSkewCorrection = nil;
}
#pragma mark -
- (void) imageTunningUpControllerChangedImageTo:(UIImage *) image {
	self.imageToOperateOn = image;
}
//--------------------------------------------------------------------------------------------
- (void)dealloc {
	[imageToOperateOn release];
	[doSkewCorrection release];
	[doConvertToGrayScale release];
	
	[viewWithSelectedImage release];
    [super dealloc];
}
#pragma mark -
//--------------------------------------------------------------------------------------------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if(buttonIndex != [actionSheet cancelButtonIndex]){
		NSString *clickedTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
		UIImagePickerControllerSourceType sType = UIImagePickerControllerSourceTypeCamera;
		if([clickedTitle isEqualToString:NSLocalizedString(@"Photo library", nil)]){
			sType = UIImagePickerControllerSourceTypePhotoLibrary;
		}
		UIImagePickerController *controller = [[UIImagePickerController alloc] init];
		controller.sourceType = sType;
		controller.delegate = self;
		[self presentModalViewController:controller animated:YES];
		[controller release];
	}
}
@end
